//
//  CZMainViewController.swift
//  swiftweibo1.0
//
//  Created by 许建安 on 15/11/23.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {
    
    //监听按钮的点击事件
    func composeClick() {
        print(__FUNCTION__)
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.tintColor = UIColor.orangeColor()

        //设置首页
        let homeVC = CZHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //设置消息
        let messageVc = CZMessageViewController()
        addChildViewController(messageVc, title: "消息", imageName: "tabbar_message_center")
        
        //占位控制器
        addChildViewController(UIViewController())
//        print("2")
        
        //设置我
        let profileVC = CZProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
        //设置发现
        let discoverVC = CZDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置按钮的frame
        let width =  tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2*width - 5, y: 0, width: width+10, height: tabBar.bounds.height)
        
        
        composeButton.frame = frame
    }
    

    
    //自定义添加tabbar子控制器
    private func addChildViewController(controller: UIViewController,title: String,imageName: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        //拼接高亮
        let highlightedImageName = imageName + "_highlighted"
        //让选中的图片使用原来的颜色
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }

    //按钮的懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置参数
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到tabbar中
        self.tabBar.addSubview(button)
//        print("1")
        
        //添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()
    
    
    
    
    
    

}
